import api from "../../utils/api_util";
import baseUtil from "../../utils/base_util";
import tips from "../../utils/tip";

const app = getApp();
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        detailData:'',
    },
    onLoad: function (options) {
        var that = this;
        let intervarID = setInterval(function () {
            var leftTime = (new Date('2019-2-03 15:46:13'.replace(/-/g, "/")).getTime()) - (new Date()); //计算剩余的毫秒数
            var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10);
            var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10);
            var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);
            var seconds = parseInt(leftTime / 1000 % 60, 10);
            days = that.checkTime(days);
            hours = that.checkTime(hours);
            minutes = that.checkTime(minutes);
            seconds = that.checkTime(seconds);
            that.setData({
                clock: days + "天" + hours + "小时" + minutes + "分" + seconds + "秒"
            })
            if (days == '00' && hours == '00' && minutes == '00' && seconds == '00') {
                clearInterval(intervarID);
            }
        }, 1000)
    },
    onShow: function () {
        this.getOrderList();
    },
    checkTime(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    },
    //订单详情
    getOrderList() {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.orderDetail, {
            method: 'GET',
            data: {
                id:baseUtil.getCurrentUrlParam('orderId'),
                // id:120,
            },
            success: function (res) {

                res.data.createTime = baseUtil.formatDateTime(res.data.createTime);

                that.setData({
                    detailData:res.data
                })
            },
        })
    },
    /**
     * 支付
     */

    //取消订单
    cancelOrderPost: function () {
        let that = this;

        wx.showModal({
            title: '提示',
            content: '确定删除要取消订单吗？',
            success(res) {
                if (res.confirm) {
                    api.wxRequest.apiRequest(that, api.urls.cancelOrder, {
                        method: 'POST',
                        data: {
                            orderId: baseUtil.getCurrentUrlParam('orderId'),
                            v:"1.0.0"
                        },
                        success: function (res) {
                            tips.alert('取消订单成功~');

                            that.getOrderList();
                        },
                    })
                } else if (res.cancel) {
                }
            }
        })

    },

    //微信支付
    wxPay: function () {
        let that =this;
        let ntr =Math.random().toString(36).substr(2);

        api.wxRequest.apiRequest(that, api.urls.wxPay, {
            method: 'POST',
            data: {
                nonceStr: ntr, //随机数
                signature:'56135Xfsdk', //签名
                orderId: that.data.detailData.id,
                payChannels: "weixinPay",
                integral: (that.data.detailData.payIntegral ? that.data.detailData.payIntegral : 0),
                price: that.data.detailData.payPrice,
                v:'1.0.0'
            },
            success: function (res) {
                wx.requestPayment({
                    'timeStamp': (res.data.timeStamp).toString(),
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': res.data.signType,
                    'paySign': res.data.sign,
                    success: function (res) {
                        that.getOrderList();
                    },
                    fail: function (res) {
                        tips.alert('支付已取消');
                    }
                })
            },
        })

    },

    jumpPage1:function (url,type) {
        baseUtil.pageJump('/pages/Index/Index', true);

    },
    jumpPage2:function (url,type) {
        baseUtil.pageJump('/pages/OrderList/OrderList', true);

    }
})